﻿//========================================================= 
/**@file ICmdDriver.h
 * @brief 将CmdDriver导出为DLL驱动
 * 
 * @date 2016-07-03   16:26:39
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_ICMDDRIVER_H_
#define _LIBZHOUYB_ICMDDRIVER_H_
//--------------------------------------------------------- 
#include "../driver/CommandDriver.h"
using zhou_yb::application::driver::ComplexCommand;
using zhou_yb::application::driver::CommandCollection;
using zhou_yb::application::driver::CommandDriver;

#include "IConverter.h"

#include "../../extension/ability/ObjectPool.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace tools {
//--------------------------------------------------------- 
/// 给CommonCmdDriver添加ICommonDriver接口的适配器
template<class TCommandCollection, class TArgParser>
class CommandDriverAppender : public CommandDriver<TArgParser>
{
protected:
    TCommandCollection _cmdDriver;
public:
    CommandDriverAppender()
    {
        list_t<Ref<ComplexCommand> > cmds;
        _cmdDriver.GetCommand("", cmds);
        CommandCollection::RegisteCommand(*this, cmds);
    }
};
//--------------------------------------------------------- 
/// 需要导出的类全局对象变量名
#define CMD_DRIVER_NAME(name) _##name##_driver
/* 直接使用IConvert接口来导出 */
#define CMD_DRIVER_EXPORTS(name) \
bool name##_TransmitCommand(const char* sCmd, ByteBuilder& sRlt, const char* sArg) \
{ \
    return _##name##_driver.TransmitCommand(sCmd, sArg, sRlt); \
} \
CONVERTER_EXPORT(name##_TransmitCommand)
//--------------------------------------------------------- 
typedef size_t(DRIVER_CALL *DLL_FUNCTION(CreateObject))();
typedef void (DRIVER_CALL *DLL_FUNCTION(DestoryObject))(size_t hInstance);
typedef ComplexCommand*(DRIVER_CALL *DLL_FUNCTION(LookUp))(size_t hInstance, const char* sCmd);

/// 导出CommandCollection
#define CMD_COLLECTION_EXPORTS(clsName, moduleName) \
zhou_yb::extension::ability::ObjectPool<clsName> CMD_DRIVER_NAME(moduleName); \
DRIVER_EXPORT size_t DRIVER_CALL moduleName##_CreateObject() \
{ \
    return CMD_DRIVER_NAME(moduleName).CreateObject(); \
} \
DRIVER_EXPORT void DRIVER_CALL moduleName##_DestoryObject(size_t hInstance) \
{ \
    CMD_DRIVER_NAME(moduleName).DestoryObject(hInstance); \
} \
DRIVER_EXPORT ComplexCommand* DRIVER_CALL moduleName##_LookUp(size_t hInstance, const char* sCmd) \
{ \
    clsName * pObj = CMD_DRIVER_NAME(moduleName).InstanceOf(hInstance); \
    if(pObj == NULL) \
        return NULL; \
    Ref<ComplexCommand> complexCmd = pObj->LookUp(sCmd); \
    if(complexCmd.IsNull()) return NULL; \
    return &(*complexCmd); \
}
//--------------------------------------------------------- 
} // namespace tools
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_ICMDDRIVER_H_
//========================================================= 